package com.system.dao;

import com.am.base.BaseDao;
import com.system.uitls.CommonUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class ApproveDao extends BaseDao
{
  public Map<String, String> update(Map<String, Object> map)
  {
    List sqlList = new ArrayList();
    List params = new ArrayList();

    Map result = new HashMap();
    try {
      String id = CommonUtil.nullToEmptyString(map.get("id"));
      String create_time = CommonUtil.nullToEmptyString(map.get("create_time"));
      String approve_result = CommonUtil.nullToEmptyString(map.get("approve_result"));
      String approve_opinion = CommonUtil.nullToEmptyString(map.get("approve_opinion"));
      Map parmsMap = getApproveParms(id);
      String out_storage_id = (String)parmsMap.get("out_storage_id");
      String place = (String)parmsMap.get("place");
      String type = (String)parmsMap.get("type");

      if (Integer.parseInt(approve_result) == 0) {
        sqlList.add("update sys_approve set create_time=?,approve_result=?,approve_opinion=?,status=? where id=?");
        params.add(new Object[] { create_time, approve_result, approve_opinion, "1", id });

        sqlList.add("update sys_drug_history set status=? where out_storage_id=?");
        params.add(new Object[] { approve_result, out_storage_id });

        batchInTransaction(sqlList, params);
        result.put("type", "success");
        result.put("value", "审批成功");

        return result;
      }

      sqlList.add("update sys_approve set create_time=?,approve_result=?,approve_opinion=?,status=? where id=?");
      params.add(new Object[] { create_time, approve_result, approve_opinion, approve_result, id });

      List list = getDrugHistoryData(out_storage_id);
      for (int i = 0; i < list.size(); i++) {
        Map drugMap = (Map)list.get(i);
        String drug_id = CommonUtil.nullToEmptyString(drugMap.get("drug_id"));
        String quantity = CommonUtil.nullToEmptyString(drugMap.get("quantity"));
        Map numMap = getDrugNum(drug_id);
        String drugstore_num = CommonUtil.nullToEmptyString(numMap.get("drugstore_num"));
        String storeroom_num = CommonUtil.nullToEmptyString(numMap.get("storeroom_num"));
        if (Integer.parseInt(type) == 4)
        {
          if (Integer.parseInt(place) == 0) {
            sqlList.add("UPDATE sys_drug SET drugstore_num=? WHERE id=?");
            params.add(new Object[] { quantity, drug_id });
          } else {
            sqlList.add("UPDATE sys_drug SET storeroom_num=? WHERE id=?");
            params.add(new Object[] { quantity, drug_id });
          }
        } else if (Integer.parseInt(type) == 1)
        {
          sqlList.add("UPDATE sys_drug SET drugstore_num=? WHERE id=?");
          params.add(new Object[] { Integer.valueOf(Integer.parseInt(drugstore_num) + Integer.parseInt(quantity)), drug_id });

          sqlList.add("UPDATE sys_drug SET storeroom_num=? WHERE id=?");
          params.add(new Object[] { Integer.valueOf(Integer.parseInt(storeroom_num) - Integer.parseInt(quantity)), drug_id });
        }
        else {
          sqlList.add("UPDATE sys_drug SET storeroom_num=? WHERE id=?");
          params.add(new Object[] { Integer.valueOf(Integer.parseInt(storeroom_num) - Integer.parseInt(quantity)), drug_id });
        }
      }

      sqlList.add("update sys_drug_history set status=? where out_storage_id=?");
      params.add(new Object[] { "1", out_storage_id });

      batchInTransaction(sqlList, params);
      result.put("type", "success");
      result.put("value", "审批成功");
    }
    catch (Exception ex) {
      logger.error("ApproveDao[update]-->sql:" + ex.getMessage());
      result.put("type", "fail");
      result.put("value", "审批失败");
    }
    return result;
  }

  public Map<String, String> getApproveParms(String sys_approve_id)
    throws Exception
  {
    String sql = "SELECT out_storage_id,type,create_time,approve_result,approve_opinion FROM sys_approve WHERE id=?";
    List list = getSimpleJdbc().queryForList(sql, new Object[] { sys_approve_id });
    Map map = (Map)list.get(0);
    String out_storage_id = CommonUtil.nullToEmptyString(map.get("out_storage_id"));
    String type = CommonUtil.nullToEmptyString(map.get("type"));
    String create_time = CommonUtil.nullToEmptyString(map.get("create_time"));
    String approve_result = CommonUtil.nullToEmptyString(map.get("approve_result"));
    String approve_opinion = CommonUtil.nullToEmptyString(map.get("approve_opinion"));

    sql = "SELECT place FROM sys_out_storage WHERE id=?";
    list = getSimpleJdbc().queryForList(sql, new Object[] { out_storage_id });
    map = (Map)list.get(0);
    String place = CommonUtil.nullToEmptyString(map.get("place"));

    Map parmsMap = new HashMap();
    parmsMap.put("out_storage_id", out_storage_id);
    parmsMap.put("type", type);
    parmsMap.put("place", place);
    parmsMap.put("create_time", create_time);
    parmsMap.put("approve_result", approve_result);
    parmsMap.put("approve_opinion", approve_opinion);
    return parmsMap;
  }

  public List<Map<String, Object>> getDrugHistoryData(String out_storage_id)
  {
    String sql = "SELECT drug_id,quantity FROM sys_drug_history WHERE out_storage_id=?";
    List list = getSimpleJdbc().queryForList(sql, new Object[] { out_storage_id });
    return list;
  }

  public Map<String, String> getDrugNum(String drug_id)
  {
    String sql = "SELECT drugstore_num,storeroom_num FROM sys_drug WHERE id=?";
    List list = getSimpleJdbc().queryForList(sql, new Object[] { drug_id });
    Map map = (Map)list.get(0);
    Map parmsMap = new HashMap();
    String drugstore_num = CommonUtil.nullToEmptyString(map.get("drugstore_num"));
    String storeroom_num = CommonUtil.nullToEmptyString(map.get("storeroom_num"));
    parmsMap.put("drugstore_num", drugstore_num);
    parmsMap.put("storeroom_num", storeroom_num);
    return parmsMap;
  }
}